<?php
/*DEE_DESC
 $templ=array(
	//Название шаблона в списке ОБЯЗАТЕЛЬНОЕ
	'title'=>'Сравнение товаров'
	);
//DEE_DESC_END*/

	$view->meta_robots = 'noindex,follow';
	
	$heads = $chars = '';
	$characts =	$fields = array();
	$i = 1;

 	$compare = isset( $_COOKIE['compare'] ) ? $_COOKIE['compare'] : '' ;
	$product_codes = $compare ? json_decode( $compare ) : array();
	
	if( count( $product_codes ) )
	{
		$products = getProductList( array( 'where' => '`product_code` IN ( '.implode( ', ', $product_codes ).' )' ) );
		$fields['Цена, руб.'] = 1;
		
		foreach( $products['list'] as $pr )
		{
			$heads .= '	<th>
							<div class="global-photo">
								<span product_code="'.$pr->product_code.'" class="comparison-goods__delete">&nbsp;</span>
								<img alt="" src="'.getImageSrcNew( $pr->product_code, $size = 'f' ).'" />
							</div>
							<h3><a href="/'.$pr->url.'">'.$pr->prefix.' '.$pr->brand.' '.$pr->name.'</a></h3>
						</th>';

			$conn->query('SELECT `f`.`name`, `v`.`value` FROM `store_p_charact_datas` AS `d`
							INNER JOIN `store_p_charact_values` AS `v` ON `v`.`id` = `d`.`value_id`
								INNER JOIN `store_p_charact_fields` AS `f` ON `f`.`id` = `d`.`field_id`
									WHERE `d`.`product_code` = '.$pr->product_code, false , 1 );
			

			$characts[$i]['Цена, руб.'] = getPriceFormat( $pr->price_retail );
			
			while( $row = $conn->fetch(1) )
			{
				$field = trim($row->name);
				$characts[$i][$field] = $row->value;
				$fields[$field] = 1;
			}
			$i++;
		}
		
		foreach( $fields as $field => $on )
		{
			$chars .= '<tr>
							<td>'.$field.'</td>';
							
			for( $p = 1; $p <= $products['count']; $p++ )
			{
				$value = isset( $characts[$p][$field] ) ? $characts[$p][$field] : '-';
				$value = str_replace( '|' , ',<br />', $value );
				$chars .= '<td>'.$value.'</td>';
				
			}
			
			$chars .= '</tr>';
		}

		$result = '<div class="comparison-goods-list">
						<table>
							<thead>
								<tr>
									<th>&nbsp;</th>
									'.$heads.'
								</tr>
							</thead>
							<tbody>
								'.$chars.'
							</tbody>
						</table>
					</div>';
	}
	else
		$result = 'Для сравления нужно выбрать как минимум два товара';
		
	$view->gText .= $view->printH1('s').$result;
	
